package com.yj.web.controllers.admin;

import com.yj.web.annotation.LoginRequired;
import com.yj.web.dao.mapper.AdminUserMapper;
import com.yj.web.dao.po.admin.*;
import com.yj.web.model.BaseWebResult;
import com.yj.web.service.MessageService;
import com.yj.web.service.OperationService;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by yanlinyang on 2017/7/25.
 */
@Controller
@RequestMapping("/admin")
public class OperationController extends BaseController {
    private static final Log logger = LogFactory.getLog(OperationController.class);

    @Autowired
    private OperationService operationService;
    @Autowired
    private MessageService messageService;

    @Autowired
    AdminUserMapper adminUserMapper;

    @LoginRequired
    @RequestMapping(value="/listBanner", method = RequestMethod.POST)
    public @ResponseBody
    BaseWebResult listBanner(@RequestParam("title") String title,
                               @RequestParam("type") Integer type,
                               @RequestParam("status") Integer status,
                               @RequestParam(value = "page", defaultValue = "1") int page, @RequestParam(value = "size", defaultValue = "10") int size) {
        BaseWebResult result = new BaseWebResult();
        List<Banner> banners = null;
        try {
            banners = operationService.listBanner(title, type, status, page, size);
        } catch (Exception e) {
            result.setCd(1);
            result.setMsg("系统错误");
        }
        result.setData(banners);
        return result;
    }

    @LoginRequired
    @RequestMapping(value="/countBanner", method = RequestMethod.POST)
    public @ResponseBody BaseWebResult countBanner(@RequestParam("title") String title,
                                                   @RequestParam("type") Integer type,
                                                   @RequestParam("status") Integer status) {
        BaseWebResult result = new BaseWebResult();
        Integer num = operationService.countBanner(title, type, status);
        result.setData(num);
        return result;
    }

    @LoginRequired
    @RequestMapping(value = "/deleteBannerById", method = RequestMethod.POST)
    public @ResponseBody BaseWebResult deleteBannerById(@RequestParam("id") Integer id) {
        BaseWebResult result = new BaseWebResult();
        int b = operationService.deleteBannerById(id);
        result.setData(b);
        return result;
    }

    @LoginRequired
    @RequestMapping(value="/insertBanner", method = RequestMethod.POST)
    public @ResponseBody BaseWebResult insertBanner(@ModelAttribute("banner") Banner banner) {
        BaseWebResult result = new BaseWebResult();

        try {
            operationService.insertBanner(banner);
            result.setMsg("增加成功");
        } catch (Exception e) {
            result.setCd(1);
            result.setMsg("系统错误");
        }
        return result;
    }

    @LoginRequired
    @RequestMapping(value = "/updateBanner", method = RequestMethod.POST)
    public @ResponseBody BaseWebResult updateBanner(@ModelAttribute("banner") Banner banner) {
        BaseWebResult result = new BaseWebResult();
        try {
            operationService.updateBanner(banner);
            result.setMsg("更新成功");
        } catch (Exception e) {
            result.setCd(1);
            result.setMsg("系统错误");
        }
        return result;
    }

    @LoginRequired
    @RequestMapping(value = "/updateBannerStatus", method = RequestMethod.POST)
    public @ResponseBody BaseWebResult updateBannerStatus(@RequestParam("id") Integer id, @RequestParam("status") Integer status) {
        BaseWebResult result = new BaseWebResult();
        try {
            boolean flag = operationService.updateBannerStatus(id, status);
            if (false == flag) {
                result.setCd(1);
                result.setMsg("系统错误");
            } else {
                result.setMsg("Banner状态更新成功");
            }
        } catch (Exception e) {
            result.setCd(1);
            result.setMsg("系统错误");
        }
        return result;
    }

    @LoginRequired
    @RequestMapping(value = "/getBannerById", method = RequestMethod.POST)
    public @ResponseBody BaseWebResult getBannerById(@RequestParam("id") Integer id) {
        BaseWebResult result = new BaseWebResult();
        try {
            Banner banner = operationService.getBannerById(id);
            result.setData(banner);
        } catch (Exception e) {
            result.setCd(1);
            result.setMsg("系统错误");
        }
        return result;
    }

    @LoginRequired
    @RequestMapping(value = "/generatePosition", method = RequestMethod.POST)
    public @ResponseBody BaseWebResult generatePosition(@RequestParam("type") int type) {
        BaseWebResult result = new BaseWebResult();
        try {
            int position = operationService.generatePosition(type);
            result.setData(position);
        } catch (Exception e) {
            result.setCd(1);
            result.setMsg("系统错误");
        }
        return result;
    }


    @LoginRequired
    @RequestMapping(value="/listArticle", method = RequestMethod.POST)
    public @ResponseBody
    BaseWebResult listArticle(@RequestParam("title") String title,
                             @RequestParam("status") Integer status,
                             @RequestParam(value = "page", defaultValue = "1") int page, @RequestParam(value = "size", defaultValue = "10") int size) {
        BaseWebResult result = new BaseWebResult();
        List<Article> articles = new ArrayList<>();
        try {
            articles = operationService.listArticle(title, status, page, size);
        } catch (Exception e) {
            result.setCd(1);
            result.setMsg("系统错误");
        }
        result.setData(articles);
        return result;
    }

    @LoginRequired
    @RequestMapping(value="/countArticle", method = RequestMethod.POST)
    public @ResponseBody BaseWebResult countArticle(@RequestParam("title") String title,
                                                   @RequestParam("status") Integer status) {
        BaseWebResult result = new BaseWebResult();
        Integer num = operationService.countArticle(title, status);
        result.setData(num);
        return result;
    }

    @LoginRequired
    @RequestMapping(value = "/deleteArticleById", method = RequestMethod.POST)
    public @ResponseBody BaseWebResult deleteArticleById(@RequestParam("id") int id) {
        BaseWebResult result = new BaseWebResult();
        int b = operationService.deleteArticleById(id);
        result.setData(b);
        return result;
    }

    @LoginRequired
    @RequestMapping(value="/insertArticle", method = RequestMethod.POST)
    public @ResponseBody BaseWebResult insertArticle(@ModelAttribute("article") Article article) {
        BaseWebResult result = new BaseWebResult();

        try {
            operationService.insertArticle(article);
            result.setMsg("增加成功");
        } catch (Exception e) {
            result.setCd(1);
            result.setMsg("系统错误");
        }
        return result;
    }

    @LoginRequired
    @RequestMapping(value = "/updateArticle", method = RequestMethod.POST)
    public @ResponseBody BaseWebResult updateArticle(@ModelAttribute("article") Article article) {
        BaseWebResult result = new BaseWebResult();
        try {
            operationService.updateArticle(article);
            result.setMsg("更新成功");
        } catch (Exception e) {
            result.setCd(1);
            result.setMsg("系统错误");
        }
        return result;
    }

    @LoginRequired
    @RequestMapping(value = "/updateArticleStatus", method = RequestMethod.POST)
    public @ResponseBody BaseWebResult updateArticleStatus(@RequestParam("id") Integer id, @RequestParam("status") Integer status) {
        BaseWebResult result = new BaseWebResult();
        try {
            boolean flag = operationService.updateArticleStatus(id, status);
            if (false == flag) {
                result.setCd(1);
                result.setMsg("系统错误");
            } else {
                result.setMsg("Article状态更新成功");
            }
        } catch (Exception e) {
            result.setCd(1);
            result.setMsg("系统错误");
        }
        return result;
    }

    @LoginRequired
    @RequestMapping(value = "/getArticleById", method = RequestMethod.POST)
    public @ResponseBody BaseWebResult getArticleById(@RequestParam("id") Integer id) {
        BaseWebResult result = new BaseWebResult();
        try {
            Article article = operationService.getArticleById(id);
            result.setData(article);
        } catch (Exception e) {
            result.setCd(1);
            result.setMsg("系统错误");
        }
        return result;
    }

    //
    @LoginRequired
    @RequestMapping(value="/listHelpDoc", method = RequestMethod.POST)
    public @ResponseBody
    BaseWebResult listHelpDoc(@RequestParam(value = "page", defaultValue = "1") int page, @RequestParam(value = "size", defaultValue = "10") int size) {
        BaseWebResult result = new BaseWebResult();
        List<HelpDoc> docs = null;
        try {
            docs = operationService.listHelpDoc(page, size);
        } catch (Exception e) {
            result.setCd(1);
            result.setMsg("系统错误");
        }
        result.setData(docs);
        return result;
    }

    @LoginRequired
    @RequestMapping(value="/countHelpDoc", method = RequestMethod.POST)
    public @ResponseBody BaseWebResult countHelpDoc() {
        BaseWebResult result = new BaseWebResult();
        Integer num = operationService.countHelpDoc();
        result.setData(num);
        return result;
    }

    @LoginRequired
    @RequestMapping(value = "/deleteHelpDocById", method = RequestMethod.POST)
    public @ResponseBody BaseWebResult deleteHelpDocById(@RequestParam("id") int id) {
        BaseWebResult result = new BaseWebResult();
        int b = operationService.deleteHelpDocById(id);
        result.setData(b);
        return result;
    }

    @LoginRequired
    @RequestMapping(value="/insertHelpDoc", method = RequestMethod.POST)
    public @ResponseBody BaseWebResult insertHelpDoc(@ModelAttribute("doc") HelpDoc doc) {
        BaseWebResult result = new BaseWebResult();

        try {
            operationService.insertHelpDoc(doc);
            result.setMsg("增加成功");
        } catch (Exception e) {
            result.setCd(1);
            result.setMsg("系统错误");
        }
        return result;
    }

    @LoginRequired
    @RequestMapping(value = "/updateHelpDoc", method = RequestMethod.POST)
    public @ResponseBody BaseWebResult updateHelpDoc(@ModelAttribute("doc") HelpDoc doc) {
        BaseWebResult result = new BaseWebResult();
        try {
            boolean flag = operationService.updateHelpDoc(doc);
            String msg = flag == true?"更新成功":"更新失败";
            result.setMsg(msg);
        } catch (Exception e) {
            result.setCd(1);
            result.setMsg("系统错误");
        }
        return result;
    }

    @LoginRequired
    @RequestMapping(value = "/updateHelpDocStatus", method = RequestMethod.POST)
    public @ResponseBody BaseWebResult updateHelpDocStatus(@RequestParam("id") Integer id, @RequestParam("status") Integer status) {
        BaseWebResult result = new BaseWebResult();
        try {
            boolean flag = operationService.updateHelpDocStatus(id, status);
            if (false == flag) {
                result.setCd(1);
                result.setMsg("系统错误");
            } else {
                result.setMsg("HelpDoc状态更新成功");
            }
        } catch (Exception e) {
            result.setCd(1);
            result.setMsg("系统错误");
        }
        return result;
    }

    @LoginRequired
    @RequestMapping(value = "/getHelpDocById", method = RequestMethod.POST)
    public @ResponseBody BaseWebResult getHelpDocById(@RequestParam("id") Integer id) {
        BaseWebResult result = new BaseWebResult();
        try {
            HelpDoc helpDoc = operationService.getHelpDocById(id);
            result.setData(helpDoc);
        } catch (Exception e) {
            result.setCd(1);
            result.setMsg("系统错误");
        }
        return result;
    }

    //
    @LoginRequired
    @RequestMapping(value="/listCustomerService", method = RequestMethod.POST)
    public @ResponseBody
    BaseWebResult listCustomerService(@RequestParam(value = "page", defaultValue = "1") int page, @RequestParam(value = "size", defaultValue = "10") int size) {
        BaseWebResult result = new BaseWebResult();
        List<CustomerService> services = null;
        try {
            services = operationService.listCustomerService(page, size);
        } catch (Exception e) {
            result.setCd(1);
            result.setMsg("系统错误");
        }
        result.setData(services);
        return result;
    }

    @LoginRequired
    @RequestMapping(value="/countCustomerService", method = RequestMethod.POST)
    public @ResponseBody BaseWebResult countCustomerService() {
        BaseWebResult result = new BaseWebResult();
        Integer num = operationService.countCustomerService();
        result.setData(num);
        return result;
    }

    @LoginRequired
    @RequestMapping(value = "/findCustomerServiceById", method = RequestMethod.POST)
    public @ResponseBody BaseWebResult findCustomerServiceById(@RequestParam("id") String id) {
        BaseWebResult result = new BaseWebResult();
        try {
            CustomerService service = operationService.findCustomerServiceById(id);
            result.setData(service);
        } catch (Exception e) {
            result.setCd(1);
            result.setMsg("系统错误");
        }
        return result;
    }

    @LoginRequired
    @RequestMapping(value = "/findCustomerServiceByIdFinished", method = RequestMethod.POST)
    public @ResponseBody BaseWebResult findCustomerServiceByIdFinished(@RequestParam("id") String id) {
        BaseWebResult result = new BaseWebResult();
        try {
            CustomerService service = operationService.findCustomerServiceByIdFinished(id);
            result.setData(service);
        } catch (Exception e) {
            result.setCd(1);
            result.setMsg("系统错误");
        }
        return result;
    }

    @LoginRequired
    @RequestMapping(value = "/updateCustomerService", method = RequestMethod.POST)
    public @ResponseBody BaseWebResult updateCustomerService(HttpServletRequest request, @ModelAttribute("service") CustomerService service) {
        BaseWebResult result = new BaseWebResult();
        try {
            long employeeId = getHostId(request);
            service.setProcessor_id(employeeId);
            service.setUpdate_id(Long.valueOf(employeeId).toString());
            operationService.updateCustomerService(service);
            result.setMsg("更新成功");
        } catch (Exception e) {
            result.setCd(1);
            result.setMsg("系统错误");
        }
        return result;
    }

//    @LoginRequired
//    @RequestMapping(value = "/listAllUserPhone", method = RequestMethod.POST)
//    public @ResponseBody BaseWebResult listAllUserPhone() {
//        BaseWebResult result = new BaseWebResult();
//        try {
//            List<String> allPhones = adminUserMapper.listAllUserPhone();
//            result.setData(allPhones);
//        } catch (Exception e) {
//            result.setCd(1);
//            result.setMsg("系统错误");
//        }
//        return result;
//    }

    @LoginRequired
    @RequestMapping(value = "/listAllMessage", method = RequestMethod.POST)
    public @ResponseBody BaseWebResult listMessageByReceiver(@RequestParam(value = "page", defaultValue = "1") int page,
                                                             @RequestParam(value = "size", defaultValue = "10") int size) {
        BaseWebResult result = new BaseWebResult();
        try {
            List<Message> msgs = messageService.listAllMessage(page, size);
            if (null != msgs) {
                result.setData(msgs);
            }
        } catch (Exception e) {
            e.printStackTrace();
            result.setCd(1);
            result.setMsg("系统错误");
        }
        return result;
    }

    @LoginRequired
    @RequestMapping(value = "/countMessage", method = RequestMethod.POST)
    public @ResponseBody BaseWebResult countMessage() {
        BaseWebResult result = new BaseWebResult();
        try {
            int num = messageService.countMessage();
            result.setData(num);
        } catch (Exception e) {
            e.printStackTrace();
            result.setCd(1);
            result.setMsg("系统错误");
        }
        return result;
    }
}
